<?php
//用户登录检测
include_once('admin_global.php');
$db->Get_user_shell_check($uid, $shell);//检测用户是否登录
$db->Get_user_type_check($uid, $shell);    //检测用户权限

//显示树形栏目分类
$query=$db->select("category","*","");
//将查询结果存储到数组中
while($row=$db->fetch_array($query)){
    $data[]=$row;
}

$cateID = $_GET['id'];     //通过get方式取得栏目ID
$tree = new Tree($data);       //树形类实例化
$gettree = $tree->get_tree(0, "<option value=\$id \$selected>\$spacer\$cateName</option>", $cateID);  //得到树形结构

//查看该栏目下是否有文章，有就读取出来，一般只能有一篇文章
$query=$db->select("news","*","newsCID=$cateID");
$nums=$db->db_num_rows();
if($nums==0){
    $rs = array('newsTitle' => '', 'newsAuthor' => '', 'newsSource' => '', 'newsKeyWords' => '', 'newsContent' => '');
}else{
    $rs=$db->fetch_array();
}

//将文章内容写入数据库或修改数据库内容
if(isset($_POST['addPageNews'])){
    //获取POST数组变量 
    $newsCID=$_POST['pid'];
    $newsTitle=trim($_POST['newsTitle']);
    $newsKeyWords = $_POST['newsKeyWords'];
    $newsAuthor = $_POST['newsAuthor'];
    $newsSource = $_POST['newsSource'];
    if (!empty($_POST['isTop'])) {
        $isTop = 1;
    } else {
        $isTop = 0;
    }
    if (!empty($_POST['isRed'])) {
        $isRed = 1;
    } else {
        $isRed = 0;
    }
    $isLinkCate=1;
    $newsSendTime = time();
    $newsUpdateTime=$newsSendTime;
    //判断是否有图片
    $newsContentTMP = $_POST['content'];
    $newsContentJJ=  mb_substr(strip_tags($newsContentTMP),0,90,'utf-8');
    $patten = '/<img.+src=\"?(.+\.(jpg|jpeg|bmp|bnp|png))\"?.+>/i';
    if (preg_match($patten, $newsContentTMP, $matche)) {
        $newsImagePath = $matche[1];
    } else {
        $newsImagePath = "";
    }
    if (!get_magic_quotes_gpc()) {
        $newsContent = addslashes($_POST['content']);
    } else {
        $newsContent = $_POST['content'];
    }
    if($nums==0){
        //写入数据库
        $rows1=$db->insert("news","newsID,newsCID,newsAuthor,newsTitle,newsSource,isTop,isRed,isLinkCate,newsSendTime,newsUpdateTime,newsImagePath,newsKeyWords,newsContent,newsContentJJ","null,$newsCID,'$newsAuthor','$newsTitle','$newsSource',$isTop,$isRed,$isLinkCate,$newsSendTime,$newsUpdateTime,'$newsImagePath','$newsKeyWords','$newsContent','$newsContentJJ'");
        $linkNewsID = strval($db->insert_id());
        $rows2=$db->update("category","linkNewsID=$linkNewsID","id=$cateID");
        if($rows1>0 AND $rows2>0){
            echo "<script>alert('单页栏目文章添加成功！');window.location.href='listNews.php';</script>";
        }else{
            echo "<script>alert('单页栏目文章添加失败！');history.go(-1);</script>";
        }
    }else{
        //修改数据库
        //先获取单页栏目对应的文章的ID
        $query=$db->select("category","*","id=$cateID");
        while ($row = $db->fetch_array($query)) {
            $newsID = $row['linkNewsID'];
        }
        $rows=$db->update("news","newsTitle='$newsTitle',newsAuthor='$newsAuthor',newsSource='$newsSource',newsUpdateTime=$newsUpdateTime,newsImagePath='$newsImagePath',newsKeyWords='$newsKeyWords',newsContent='$newsContent'","newsID=$newsID");
        if($rows>0){
            echo "<script>alert('单页栏目文章修改成功！');window.location.href='listCate.php';</script>";
        }else{
            echo "<script>alert('单页栏目文章修改失败！');history.go(-1);</script>";
        }
    }
}





?>?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>编辑单页栏目</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" /> 
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript" src="js/jquery.validate-1.13.1.js"></script>
        <!--UE编辑器-->
        <!-- 配置文件 -->
        <script type="text/javascript" src="../ueditor/ueditor.config.js"></script>
        <!-- 编辑器源码文件 -->
        <script type="text/javascript" src="../ueditor/ueditor.all.js"></script>
        <!-- 实例化编辑器 -->
        <script type="text/javascript">
            $(function(){
                var ue = UE.getEditor('container',{
                    autoHeightEnabled: true
                });
            })
        </script>
    </head>
    <script>
        $(function(){
            $("#form1").validate({
                rules:{
                    newsTitle:{
                        required:true,
                        rangelength:[6,30]
                    },
                    newsAuthor:{
                        required:true
                    }
                },
                messages:{
                    newsTitle:{
                        required:"请输入文章标题！",
                        rangelength:"文章标题不得少于6个字符！"
                    },
                    newsAuthor:{
                        required:"请输入文章作者！"
                    }
                }
            })
        })
    </script>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        您当前的位置：后台 > 文章管理 > 添编辑文章
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search"></div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                        <tr>
                                            <th colspan="2" >编辑文章</th>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">所属栏目：</td>
                                            <td width="90%">
                                                <select name="pid">
                                                    <?php print_r($gettree); ?> 
                                                </select> *不能指定为含有子栏目的栏目！
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章标题：</td>
                                            <td width="90%"><input type="text" name="newsTitle"  class="int_text" value="<?php echo $rs['newsTitle']; ?>" size="60"  maxlength="60"/> *文章标题不能少于6个字符！</td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">关键字：</td>
                                            <td width="90%"><input type="text" name="newsKeyWords"  class="int_text" value="<?php echo $rs['newsKeyWords']; ?>" size="40"  maxlength="40"/></td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章作者：</td>
                                            <td width="90%"><input type="text" name="newsAuthor"  class="int_text" value="<?php echo $rs['newsAuthor']; ?>" size="40"  maxlength="40"/> *必填</td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章来源：</td>
                                            <td width="90%"><input type="text" name="newsSource"  class="int_text" value="<?php echo $rs['newsSource']; ?>" size="40"  maxlength="40"/></td>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">是否置顶：</td>
                                            <td width="90%"><input type="checkbox" name="isTop"  class="int_text" value="6" size="40" <?php if ($rs['isTop'] == 1) {  echo "checked='checked'";  } ?> /></td>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">是否加红：</td>
                                            <td width="90%"><input type="checkbox" name="isRed"  class="int_text" value="6" size="40" <?php if ($rs['isRed'] == 1) {  echo "checked='checked'";  } ?> /></td>
                                        </tr>   
                                        <tr>
                                            <td width="10%" align="right">文章内容：</td>
                                            <td width="90%">
                                                <script id="container" name="content" style="height:300px" type="text/plain"><?php echo $rs['newsContent']; ?></script>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td colspan="2" align="center" height='30'>
                                                <input type="submit" name="addPageNews" class="btn_ipt01" value=" 确定 "/>
                                            </td>  
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
